﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LibraryO2S;
public partial class Administrator_ProductAdd : System.Web.UI.Page
{
    BindToControls objBind = new BindToControls();
    ConnectSQL cnts = new ConnectSQL();
    BizProduct objProduct = new BizProduct();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropdownlist();
        }
    }
    public void BindDropdownlist()
    {
        objBind.BinDDL(ddlCategory, cnts.GetTableWithCommandText("select * from tblProductCategories"), "CATEGORY_NAME", "CATEGORY_ID");
        objBind.BinDDL(ddlPublisher, cnts.GetTableWithCommandText("select * from tblPublish"), "PUBLISHER_NAME", "PUBLISHER_ID");
    }
    BizProductCategory category = new BizProductCategory();
    public int GetRandom()
    {
        Random rd = new Random();
        int a = rd.Next(1, 100);
        return a;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        string status = "0";
        if (cbStatus.Checked)
        {
            status = "1";
        }


        int bestSelling = 0;

        if (cbBestSelling.Checked)
        {
            bestSelling = 1;
        }
        int free = 0;
        if (cbFree.Checked)
        {
            free = 1;
        }
        int highvalue = 0;
        if (cbHighValue.Checked)
        {
            highvalue = 1;
        }
        int isnew = 0;
        if (cbNew.Checked)
        {
            isnew = 1;
        }

        try
        {





            string doc_file = "";
            if (fuDoc.HasFile)
            {
                doc_file = GetRandom() + "" + fuDoc.FileName;
                fuDoc.SaveAs(Server.MapPath("~/TaiLieu/") + doc_file);
            }
            string des_file = "";
            if (fuDeCuong.HasFile)
            {
                des_file = GetRandom() + "" + fuDeCuong.FileName;
                fuDeCuong.SaveAs(Server.MapPath("~/TaiLieu/") + des_file);
            }
            objProduct.tblProducts_Insert(txtProductName.Text
            , int.Parse(ddlPublisher.SelectedValue),
             int.Parse(ddlCategory.SelectedValue),
             DateTime.Parse(txtPublishedDate.Text),
             ddlLanguage.SelectedItem.Text,
             float.Parse(txtPrice.Text),
             string.Empty, string.Empty, free, bestSelling, highvalue, isnew,
            int.Parse(txtSortDisplay.Text),
           int.Parse(status), string.Empty,
             txtContent.Text, doc_file,
             des_file);



            divMessage.InnerHtml = O2S_Message.Success("Hệ thống", "Thực hiện thành công");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Hệ thống", "Phát hiện lỗi" + ex.Message);
        }

    }
}